package marcus.age;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import marcus.age.impl.ShortAgeFormatter;
import marcus.jpa.entities.Contact;

import org.apache.commons.lang3.time.DateUtils;
import org.joda.time.Period;
import org.joda.time.PeriodType;

public class AgeCalculator {
	
	public Map<Contact, String> getAges(List<Contact> contacts, Date date){
		TreeMap<Contact, String> t = new TreeMap<Contact, String>();
		for (Contact contact : contacts){
			Date dob = DateUtils.truncate(contact.getDob(), Calendar.DATE);
			date = DateUtils.truncate(date, Calendar.DATE);
			Period period;
			if (date.before(dob)){
				period = null;
			}
			else {
				period = new Period (dob.getTime(), date.getTime(), PeriodType.yearMonthDay());
			}
			t.put(contact,new ShortAgeFormatter().format(contact, period, date));
		}
		
		return t;
	}
}
